package myapp.mvc_pattern;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.os.Bundle;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.Editable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TextView;

public class CounterMainActivity extends Activity implements Observer, OnClickListener{

	private Button mButton1;
	private Button mButton2;
	private Button mButton3;
	private Button mButton4;
	private Button mButton5;
	private Counter_Model mModel;
	private ListView listview;
	private TextView toPay;
	private ArrayAdapter<Item> la;
	private Item selectedItem;
	private IO_Files io_storage;
	private Person nPerson;


	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_counter_main);
		mModel = new Counter_Model();

		Bundle bundleExtras = getIntent().getExtras();
		if (bundleExtras != null) {
			nPerson = (Person) getIntent().getSerializableExtra("SelectedPerson");
		}
		nPerson.addObserver(this);
		
		io_storage = new IO_Files(getApplicationContext());

		if (io_storage.getPersonFile().exists()) {
			nPerson.setList(io_storage.findPerson(nPerson).getList());
			Log.d("rec1234","blablalb");
		}
		
		la = new ArrayAdapter<Item>(this, android.R.layout.simple_list_item_1, nPerson.getList());
		
		mModel.addObserver(this);
		mButton1 = (Button) findViewById(R.id.button1);
		mButton2 = (Button) findViewById(R.id.button2);
		mButton3 = (Button) findViewById(R.id.button3);
		mButton4 = (Button) findViewById(R.id.button4);
		mButton5 = (Button) findViewById(R.id.button5);
		listview = (ListView) findViewById(R.id.listView1);
		toPay = (TextView) findViewById(R.id.textView4);
		listview.setAdapter(la);
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				selectedItem = (Item) listview.getAdapter().getItem(arg2);
				
			}
		});
		mButton1.setOnClickListener(this);
		mButton2.setOnClickListener(this);
		mButton3.setOnClickListener(this);
		mButton4.setOnClickListener(this);
		mButton5.setOnClickListener(this);
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.counter_main, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) 
		{
		case R.id.button1:
			mModel.setValueAtIndex(0);
			break;
		case R.id.button2:
			mModel.setValueAtIndex(1);
			break;
		case R.id.button3:
			Item item = nPerson.getSelectedItem(la.getPosition(selectedItem));
			item.addObserver(this);
			item.setStatePaid();
			break;
		case R.id.button4:
			 LayoutInflater factory = LayoutInflater.from(this);
			 final View textEntryView = factory.inflate(R.layout.new_person, null);
			 AlertDialog.Builder alert = new AlertDialog.Builder(this);
			 alert.setTitle("Erstellen");
			 final EditText input1 = (EditText) textEntryView.findViewById(R.id.editText1);
			 final EditText input2 = (EditText) textEntryView.findViewById(R.id.editText2);
			 alert.setView(textEntryView);
			 
			 alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
				 public void onClick(DialogInterface dialog, int whichButton) {
				   String value = input1.getText().toString();
				   int value2 = Integer.parseInt(input2.getText().toString());
				   nPerson.addItemToList(new Item(value, value2));
				   }
				 });

				 alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
				   public void onClick(DialogInterface dialog, int whichButton) {
				     // Canceled.
				   }
				 });

				 alert.show();
			break;
		case R.id.button5:
			break;
		}
	}

	@Override
	public void update(Observable observable, Object data) {
		mButton1.setText("Count : " + mModel.getValueAtIndex(0));
		mButton2.setText("Count : " + mModel.getValueAtIndex(1));
		mButton3.setText("Count : " + mModel.getValueAtIndex(2));
		toPay.setText("Zu zahlen: "+nPerson.getSumOpenItems()+" Euro");
		la.notifyDataSetChanged();
		}
	
	@Override
	protected void onStop () {
		super.onStop();
		io_storage.savePersonList2(nPerson);
	}
	@Override
	protected void onPause () {
		super.onPause();
		io_storage.savePersonList2(nPerson);
	}
	
}
